import {
    getCache
} from "@/utils/cache.js"


const request = {
    get(url, data) {
        return this.http(url, data, "GET");
    },
    post(url, data) {
        return this.http(url, data, "POST");
    },
    /**
     * 二次封装的请求
     * @param {Object} url
     * @param {Object} data
     * @param {Object} method
     */
    http(url, data, method) {
        return new Promise((resolve, reject) => {
            uni.request({
                url: uni.getStorageSync('ROOT_URL') + '/api/music/getWyyApi',
                data: {
                    url: url,
                    method: method,
                    data: data
                },
                header: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    '__userId': uni.getStorageSync('LOGIN_RESULT').loginCode,
                    '__token': uni.getStorageSync('TOKEN')
                },
                method: "POST",
                success: (response) => {
                    let res = response.data
                    if (res.code === 200 || url == '/check/music') {
                        resolve(res)
                    } else if (res.code === 401) {
                        //请重新登录
                        uni.removeStorageSync('LOGIN_RESULT')
                        uni.reLaunch({
                            url: '/pages/login/index?statusCode=401'
                        })
                    } else {
                        reject(res)
                        // showError(res);
                    }
                },
                fail: (error) => {
                    if (error && error.response) {
                        reject(error.response)
                        showError(error.response);
                    }
                },


            });
        })
    }
}

const showError = error => {
    let errorMsg = ''
    if (error.message) {
        errorMsg = error.message
    } else {
        switch (error.code) {
            case 400:
                errorMsg = '请求参数错误'
                break
            case 302:
                errorMsg = '未授权，需登录网易云音乐'
                break
            case 403:
                errorMsg = '跨域拒绝访问'
                break
            case 404:
                errorMsg = '请求地址不存在'
                break
            case 408:
                errorMsg = '请求超时'
                break
            case 500:
                errorMsg = '服务器内部错误'
                break
            case 501:
                errorMsg = '服务未实现'
                break
            case 502:
                errorMsg = '网关错误'
                break
            case 503:
                errorMsg = '服务不可用'
                break
            case 504:
                errorMsg = '网关超时'
                break
            case 505:
                errorMsg = 'HTTP版本不受支持'
                break
            default:
                errorMsg = "请求失败"
                break
        }
    }

    uni.showToast({
        title: errorMsg,
        icon: 'none',
        duration: 3000,
        complete: function() {
            setTimeout(function() {
                // if (error.code == 302) {
                //     uni.navigateTo({
                //         url: '../login/index'
                //     });
                // }
                uni.hideToast();
            }, 3000);
        }
    });
}

export default request
